﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace GUI.PhieuXuatHuy
{
    public partial class frmXuLy : Form
    {
        #region Delegate
        public delegate void GetString(Boolean capnhat);
        public GetString GetData;
        public static bool CheckClose = false;
        #endregion
        public DateTime date;
        public string manhanvien = "";
        public string tennhanvien = "";
        public string tongtien = "0";
        #region Khai báo
        public bool kiemtra = false;
        bool checkClose = false;
        bool check = false;
        Entities.PhieuXuatHuy[] phieu;
        Entities.PhieuXuatHuy phieuxuathuy;
        Entities.PhieuXuatHuy temp;
        Entities.ChiTietXuatHuy[] chitietphieuxuathuy;
        Entities.ChiTietXuatHuy[] hienthi;
        Entities.ChiTietXuatHuy[] templv;
        Entities.ChiTietXuatHuy save;
        Entities.HangHoa[] hanghoa;
        Entities.ChiTietKhoHang[] chitietkhohang;
        Entities.PhieuXuatHuy xuathuy;
        Entities.KhachHang khachhang;
        string hanhDong = "";
        string mahanghoa = "";
        string gianhap ;
        int tonsosach;
        int soluong ;
        double thanhtien ;
        string makho="";
        string tenkho = "";
        DateTime NgayLap;
        public static string maPhieuXuatHuy;

        public frmXuLy()
        {
            InitializeComponent();
            try
            {

                date = Common.StaticVariable.InterfaceObject.SelectDate();              
                Entities.CaiDatKho kho = GUI.Entitystatic.ViewFile();
                makho = kho.MaKho;
                tenkho = kho.TenKho;
                Entities.ChiTietKhoHang tem = new Entities.ChiTietKhoHang();
                tem.MaKho =  makho;
                tem.HanhDong = "ChiTietHangTheoKho";
               // chitietkhohang = Common.StaticVariable.InterfaceObject.SelectHangHoa_TrongKho(tem);
                maPhieuXuatHuy = getID();
                NgayLap = DateTime.Now;
                lblTieuDe.Text = Common.Constants.PhieuXuatHuy.TieuDeXuLyThem;
                txtMaKho.Text = Common.Constants.MaKho + makho;
                txtTenKho.Text = Common.Constants.TenKho + tenkho;
                hanhDong = Common.Constants.Insert;
            }
            catch
            {
            }
        }

        public frmXuLy(Entities.PhieuXuatHuy phieuxuathuy)
        {
            InitializeComponent();
            try
            {

                date = Common.StaticVariable.InterfaceObject.SelectDate();
                Entities.CaiDatKho kho1 = GUI.Entitystatic.ViewFile();
                makho = kho1.MaKho;
                tenkho = kho1.TenKho;
                  Entities.ChiTietKhoHang tem = new Entities.ChiTietKhoHang();
                tem.MaKho = makho;
                tem.HanhDong = "ChiTietHangTheoKho";
               // chitietkhohang = Common.StaticVariable.InterfaceObject.SelectChiTietHangHoaTheoKho(tem);
                temp = phieuxuathuy;
                //chitietphieuxuathuy = new HienThi(phieuxuathuy);
                dgvHienThi.DataSource = chitietphieuxuathuy;
               // ThanhTien();
                //TongTien();
               // lbTongSo.Content = Common.Constants.TongSo + chitietphieuxuathuy.Length;
               // lbGiaTri.Content = new Common.Utilities().FormatMoney(TongTien());
                tongtien = textBox1.Text.ToString();

                templv = chitietphieuxuathuy;
                XuLyDuLieu(phieuxuathuy);
                maPhieuXuatHuy = phieuxuathuy.MaPhieuXuatHuy;             
                lblTieuDe.Text = Common.Constants.PhieuXuatHuy.TieuDeXuLySua;
                txtMaKho.Text = Common.Constants.MaKho + makho;
                txtTenKho.Text = Common.Constants.TenKho + tenkho;
                hanhDong = Common.Constants.Update;
            }
            catch
            {
            }
        }
        #endregion

        #region xử lý list view
        string TongTien()
        {
            try
            {
                double tongtien = 0;
                hienthi = (Entities.ChiTietXuatHuy[])dgvHienThi.DataSource;
                foreach (Entities.ChiTietXuatHuy item in hienthi)
                {
                    tongtien += double.Parse(item.ThanhTien);
                }
                return tongtien.ToString();
            }
            catch
            {
                return "0";
            }
        }

        void ThanhTien()
        {
            try
            {
                double tongtien = 0;
                hienthi = (Entities.ChiTietXuatHuy[])dgvHienThi.DataSource;
                foreach (Entities.ChiTietXuatHuy item in hienthi)
                {
                    item.ThanhTien = (double.Parse(item.GiaNhap) * item.SoLuong).ToString();
                }
            }
            catch
            {
                return;
            }
        }
        #endregion


        #region Thêm Mới LV
        public void NewRow()
        {
            try
            {
                bool kt1 = false;

                int sohangtrongbang;
                hienthi = (Entities.ChiTietXuatHuy[])dgvHienThi.DataSource;
                // list view = null
                if (hienthi == null)
                {
                    string sl = "0";
                    if (txtSoLuong.Text == "")
                        sl = "1";
                    else
                        sl = txtSoLuong.Text;
                    string soluongsp = sl + soluong;

                    //giá nhập
                    string gianhap = "0";
                    gianhap = txtGiaNhap.Text;
                    //  double gianhapsp = double.Parse(gianhap) * soluong;

                    templv = new Entities.ChiTietXuatHuy[1];
                    thanhtien = double.Parse(gianhap) * soluong;
                    templv[0] = new Entities.ChiTietXuatHuy("Insert", 0, maPhieuXuatHuy, txtMaHangHoa.Text,
                         txtGiaNhap.Text,
                        int.Parse(sl), gianhap,DateTime.Now, thanhtien.ToString(), "", false);
                    dgvHienThi.DataSource = templv;
                    return;
                }

                sohangtrongbang = hienthi.Length;
                // số lượng bảng ghi khác 0
                if (sohangtrongbang != 0)
                {
                    for (int j = 0; j < sohangtrongbang; j++)
                    {
                        // nếu tìm được mã đã có trong list view
                        if (mahanghoa == hienthi[j].MaHangHoa)
                        {
                            templv = new Entities.ChiTietXuatHuy[sohangtrongbang];
                            break;
                        }
                        // nếu không tìm được
                        else
                            templv = new Entities.ChiTietXuatHuy[sohangtrongbang + 1];
                    }
                }
                // số lượng bảng ghi = 0
                else
                    templv = new Entities.ChiTietXuatHuy[sohangtrongbang + 1];
                // nếu số lượng bảng ghi khác 0
                if (templv.Length != 0)
                {
                    try
                    {
                        // số lượng bảng ghi = số lượng row list view
                        if (templv.Length == hienthi.Length)
                        {
                            for (int j = 0; j < templv.Length; j++)
                            {
                                // nếu mã = mã trong list view
                                if (mahanghoa == hienthi[j].MaHangHoa)
                                {
                                    int soluongcu = Convert.ToInt32(hienthi[j].SoLuong);
                                    string sl = "0";
                                    if (txtSoLuong.Text == "")
                                        sl = "1";
                                    else
                                        sl = txtSoLuong.Text;
                                    int soluongmoi = Convert.ToInt32(sl);
                                    int soluonghientai = soluongcu + soluongmoi;
                                    //giá nhập
                                    string gianhap = "0";
                                    gianhap = txtGiaNhap.Text;
                                    double gianhapmoi = double.Parse(gianhap);
                                    //  double gianhaphientai = gianhapmoi * soluonghientai;
                                    thanhtien = gianhapmoi * soluonghientai;
                                    templv[j] = new Entities.ChiTietXuatHuy("Insert", 0, maPhieuXuatHuy,
                                        txtMaHangHoa.Text, txtGiaNhap.Text, soluonghientai, gianhapmoi.ToString(),DateTime.Now,
                                        thanhtien.ToString(), "", false);
                                }
                                // nếu mã không = mã trong list view
                                else
                                    templv[j] = new Entities.ChiTietXuatHuy("Insert", 0, hienthi[j].MaPhieuXuatHuy, hienthi[j].MaHangHoa,
                                        hienthi[j].TenHangHoa, hienthi[j].SoLuong, hienthi[j].GiaNhap,hienthi[j].HanSuDung, hienthi[j].ThanhTien, "", false);

                            }
                        }
                        // số lượng bảng ghi không = số lượng row list view
                        else
                        {
                            for (int j = 0; j < templv.Length; j++)
                            {
                                // lấy lại số bảng ghi trên list view
                                if (j < templv.Length - 1)
                                    templv[j] = new Entities.ChiTietXuatHuy("Insert", 0, hienthi[j].MaPhieuXuatHuy, hienthi[j].MaHangHoa,
                                        hienthi[j].TenHangHoa, hienthi[j].SoLuong, hienthi[j].GiaNhap,hienthi[j].HanSuDung, hienthi[j].ThanhTien, "", false);
                                // bảng ghi cuối cùng là mới insert vào
                                else
                                {
                                    string sl = "0";
                                    if (txtSoLuong.Text == "")
                                        sl = "1";
                                    else
                                        sl = txtSoLuong.Text;
                                    string soluongsp = sl + soluong;
                                    //giá nhập
                                    string gianhap = "0";
                                    gianhap = txtGiaNhap.Text;
                                    //double gianhapsp = double.Parse(gianhap) * soluong;
                                    thanhtien = double.Parse(gianhap) * int.Parse(sl);
                                    templv[templv.Length - 1] = new Entities.ChiTietXuatHuy("Insert", 0, maPhieuXuatHuy,
                                        txtMaHangHoa.Text, txtGiaNhap.Text, int.Parse(sl), gianhap.ToString(),DateTime.Now,
                                        thanhtien.ToString(), "", false);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        string sl = "0";
                        if (txtSoLuong.Text == "")
                            sl = "1";
                        else
                            sl = txtSoLuong.Text;
                        string soluongsp = sl + soluong;
                        //giá nhập
                        string gianhap = "0";
                        gianhap = txtGiaNhap.Text;
                        // double gianhapsp = double.Parse(gianhap) * soluong;
                        thanhtien = double.Parse(gianhap) * int.Parse(sl);
                        templv[templv.Length - 1] = new Entities.ChiTietXuatHuy("Insert", 0, maPhieuXuatHuy,
                                        txtMaHangHoa.Text, txtGiaNhap.Text, int.Parse(sl), gianhap.ToString(),DateTime.Now,
                                        thanhtien.ToString(), "", false);
                    }
                    dgvHienThi.DataSource = templv;
                }
            }
            catch
            {
                string sl = "0";
                if (txtSoLuong.Text == "")
                    sl = "1";
                else
                    sl = txtSoLuong.Text;
                string soluongsp = sl + soluong;
                //giá nhập
                string gianhap = "0";
                gianhap = txtGiaNhap.Text;
                //double gianhapsp = double.Parse(gianhap) * soluong;
                thanhtien = double.Parse(gianhap) * int.Parse(sl);
                templv = new Entities.ChiTietXuatHuy[1];
                templv[0] = new Entities.ChiTietXuatHuy("Insert", 0, maPhieuXuatHuy,
                                        txtMaHangHoa.Text,txtGiaNhap.Text, int.Parse(sl), gianhap.ToString(),DateTime.Now,
                                        thanhtien.ToString(), "", false);
               dgvHienThi.DataSource= templv;
            }
            finally
            {
                //  soluong = 1;
                soluong = 0;
                mahanghoa = "";
                gianhap = "0";
                txtGiaNhap.Text = "0";
                txtMaHangHoa.Text = "";
                txtTenHangHoa.Text = "";
                //ucHienThi.lbTongSo.Content = Common.Constants.TongSo + templv.Length;
                textBox1.Text = TongTien();
                tongtien = textBox1.Text.ToString();
                txtMaHangHoa.Focus();
            }
        }
        #endregion


        #region sửa row
        int i;
        void SuaHangHoa()
        {

            if (i < 0)
                return;
            if (dgvHienThi.RowCount > 1)
            {
                maPhieuXuatHuy = dgvHienThi["MaPhieuXuatHuy", i].Value.ToString().ToUpper();
                txtMaHangHoa.Text = dgvHienThi["MaHangHoa", i].Value.ToString().ToUpper();
                txtTenHangHoa.Text = dgvHienThi["TenHangHoa", i].Value.ToString();
                txtGiaNhap.Text = dgvHienThi["GiaNhap", i].Value.ToString();
                txtSoLuong.Text = dgvHienThi["SoLuong", i].Value.ToString();
                chitietphieuxuathuy = new Entities.ChiTietXuatHuy[dgvHienThi.RowCount - 1];
                int sotang = 0;
                for (int j = 0; j < dgvHienThi.RowCount; j++)
                {
                    if (dgvHienThi["MaHangHoa", j].Value.ToString() != dgvHienThi["MaHangHoa", i].Value.ToString())
                    {
                        chitietphieuxuathuy[sotang] = new Entities.ChiTietXuatHuy("Delete", 0,
                 dgvHienThi["MaPhieuXuatHuy", j].Value.ToString(), dgvHienThi["MaHangHoa", j].Value.ToString(),
                 dgvHienThi["TenHangHoa", j].Value.ToString(), int.Parse(dgvHienThi["SoLuong", j].Value.ToString()),
              dgvHienThi["GiaNhap", j].Value.ToString(), DateTime.Parse(dgvHienThi["HanSuDung", j].Value.ToString()), thanhtien.ToString(), 
                 dgvHienThi["GhiChu", j].Value.ToString(), false);
                        sotang++;
                    }
                }
                dgvHienThi.DataSource = chitietphieuxuathuy;
            }
            else
            {
                maPhieuXuatHuy = dgvHienThi["MaPhieuXuatHuy", i].Value.ToString().ToUpper();
                txtMaHangHoa.Text = dgvHienThi["MaHangHoa", i].Value.ToString().ToUpper();
                txtTenHangHoa.Text = dgvHienThi["TenHangHoa", i].Value.ToString();
                txtGiaNhap.Text = dgvHienThi["GiaNhap", i].Value.ToString();
                dtpHSD.Text = dgvHienThi["GiaBanBuon", i].Value.ToString();
                txtGhiChu.Text = dgvHienThi["GiaBanLe", i].Value.ToString();
                txtSoLuong.Text = dgvHienThi["SoLuong", i].Value.ToString();
                dgvHienThi.DataSource = new Entities.ChiTietXuatHuy[0];
            }
        }
        #endregion

        #region Thêm Hàng Hóa
        private void txtMaHangHoa_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyData == Keys.F4)
                {
                    if (makho.Length == 0 || makho == "NULL")
                    {
                        MessageBox.Show(Common.MSG.MSG28(), Common.MSG.TieuDe);
                        return;
                    }
                    TimKiem.frmHangHoa timkiem = new TimKiem.frmHangHoa(makho);
                    timkiem.ShowDialog();
                    if (TimKiem.frmHangHoa.selectedItem != null)
                    {
                        txtMaHangHoa.Text = TimKiem.frmHangHoa.selectedItem.MaHangHoa.ToUpper();
                        txtTenHangHoa.Text = TimKiem.frmHangHoa.selectedItem.TenHangHoa;
                        gianhap = TimKiem.frmHangHoa.selectedItem.GiaNhap;
                        txtGiaNhap.Text = gianhap.ToString();
                        TimKiem.frmHangHoa.selectedItem = null;

                    }
                }
            }
            catch
            {
            }
            finally
            {
                txtSoLuong.Focus();

            }
        }
        #endregion

        #region xử lý thêm sửa
        void XuLyThemSua()
        {
            if (hanhDong == Common.Constants.Insert)
                GhiLai();
            else
                Sua();
        }
        #endregion

        #region Ghi Lại
        void GhiLai()
        {
            try
            {
                // check
                if (check)
                {
                    check = false;
                    return;
                }

               // maphieuxuathuy = maPhieuXuatHuy;
                makho = txtMaKho.Text;
                tenkho = txtTenKho.Text;

                ngaylap = DateTime.Now;
                phieuxuathuy = XuLyObject("Insert");
                chitietphieuxuathuy = (Entities.ChiTietXuatHuy[])dgvHienThi.DataSource;
                // check object
                if (phieuxuathuy == null)
                {
                    MessageBox.Show(Common.MSG.MSG35(), Common.MSG.TieuDe);
                    return;
                }
                // check object
                if (chitietphieuxuathuy == null || chitietphieuxuathuy.Length == 0)
                {
                    MessageBox.Show(Common.MSG.MSG13(lblMaHangHoa.Text), Common.MSG.TieuDe);
                    return;
                }
                // phieuxuathuy = XuLyObject(Common.Constants.Insert);
                // check conflic insert
                if (!CheckConflicInsert(phieuxuathuy))
                {
                    MessageBox.Show(Common.MSG.MSG5(phieuxuathuy.MaPhieuXuatHuy), Common.MSG.TieuDe);
                    maPhieuXuatHuy = maphieuxuathuymoi;
                    Select("");
                    return;
                }
                // check insert
                if (Common.StaticVariable.InterfaceObject.InsertUpdatePhieuXuatHuy(phieuxuathuy) == 0)
                {
                    MessageBox.Show(Common.MSG.MSG15(phieuxuathuy.MaPhieuXuatHuy), Common.MSG.TieuDe);
                   Common.StaticVariable.InterfaceObject.DeletedPhieuXuatHuy(phieuxuathuy);
                    return;
                }
                // check insert details
                for (int i = 0; i < chitietphieuxuathuy.Length; i++)
                {
                    chitietphieuxuathuy[i].MaPhieuXuatHuy = maPhieuXuatHuy;
                }
                if (Common.StaticVariable.InterfaceObject.InsertUpdateChiTietXuatHuy(chitietphieuxuathuy) == 0)
                {
                    MessageBox.Show(Common.MSG.MSG15(phieuxuathuy.MaPhieuXuatHuy), Common.MSG.TieuDe);
                    Entities.ChiTietXuatHuy delete = new Entities.ChiTietXuatHuy();
                    delete.MaPhieuXuatHuy = phieuxuathuy.MaPhieuXuatHuy;
                    Common.StaticVariable.InterfaceObject.DeletedChiTietXuatHuy(delete);
                    return;
                }
                checkClose = true;
                phieuxuathuy = null;
                check = true;
                GetData(true);
                this.Close();

            }
            catch
            {
            }
        }

        Entities.ChiTietKhoHang[] CheckListView(Entities.ChiTietXuatHuy[] hienthi)
        {
            try
            {
                Entities.ChiTietKhoHang[] ctkh = new Entities.ChiTietKhoHang[hienthi.Length];
                int sotang = 0;
                foreach (Entities.ChiTietXuatHuy item in hienthi)
                {
                    int soluongtrongkho = 0;
                    // lấy số lượng trong kho
                    foreach (Entities.ChiTietKhoHang item1 in chitietkhohang)
                    {
                        if (item1.MaHangHoa == item.MaHangHoa)
                        {
                            soluongtrongkho = int.Parse(item1.SoLuong);
                            break;
                        }
                    }
                    ctkh[sotang] = new Entities.ChiTietKhoHang();
                    ctkh[sotang].MaKho = makho;
                    ctkh[sotang].MaHangHoa = item.MaHangHoa;
                    ctkh[sotang].SoLuong = (soluongtrongkho - item.SoLuong).ToString();
                    sotang++;
                }
                return ctkh;
            }
            catch
            {
                return new Entities.ChiTietKhoHang[0];
            }
        }
        //
        #endregion

        #region Check Conflic Insert
        string maphieuxuathuymoi;
        public bool CheckConflicInsert(Entities.PhieuXuatHuy phieuxuathuy)
        {
            try
            {
                maphieuxuathuymoi = getID();
                if (maPhieuXuatHuy == maphieuxuathuymoi)
                {
                    return true;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region Sửa
        void Sua()
        {
            try
            {
                if (temp.TrangThai == true)
                {
                    return;

                }
                if (check)
                {
                    check = false;
                    return;
                }
                phieuxuathuy = XuLyObject("Update");
                chitietphieuxuathuy = (Entities.ChiTietXuatHuy[])dgvHienThi.DataSource;
                // check object
                if (phieuxuathuy == null)
                {
                    return;
                }

                // check object
                if (chitietphieuxuathuy == null || chitietphieuxuathuy.Length == 0)
                {
                    MessageBox.Show(Common.MSG.MSG35(), Common.MSG.TieuDe);
                    return;
                }
                // check conflic insert
                if (!CheckConflicUpdate())
                {
                    MessageBox.Show(Common.MSG.MSG6(), Common.MSG.TieuDe);
                    return;
                }
                // check insert
                if (Common.StaticVariable.InterfaceObject.InsertUpdatePhieuXuatHuy(phieuxuathuy) == 0)
                {
                    MessageBox.Show(Common.MSG.MSG17(phieuxuathuy.MaPhieuXuatHuy), Common.MSG.TieuDe);
                    Common.StaticVariable.InterfaceObject.DeletedPhieuXuatHuy(phieuxuathuy);
                    return;
                }
                // delete details
                Entities.ChiTietXuatHuy delete = new Entities.ChiTietXuatHuy();
                delete.MaPhieuXuatHuy = phieuxuathuy.MaPhieuXuatHuy;
                if (Common.StaticVariable.InterfaceObject.DeletedChiTietXuatHuy(delete) == 0)
                {

                }
                // check insert details
                foreach (Entities.ChiTietXuatHuy item in chitietphieuxuathuy)
                {
                    item.HanhDong = "Insert";
                }
                if (Common.StaticVariable.InterfaceObject.InsertUpdateChiTietXuatHuy(chitietphieuxuathuy) == 0)
                {
                    MessageBox.Show(Common.MSG.MSG15(phieuxuathuy.MaPhieuXuatHuy), Common.MSG.TieuDe);
                    Entities.ChiTietXuatHuy deleteerror = new Entities.ChiTietXuatHuy();
                    delete.MaPhieuXuatHuy = maPhieuXuatHuy;
                    Common.StaticVariable.InterfaceObject.DeletedChiTietXuatHuy(deleteerror);
                    return;
                }

                checkClose = true;
                phieuxuathuy = null;
                check = true;
                this.Close();

            }
            catch
            {
            }
        }
        #endregion

        #region Check Conflic Update
        public bool CheckConflicUpdate()
        {
            try
            {
                //Entities.PhieuXuatHuy[] mangchitiet = HienThi();
                //for (int i = 0; i < mangchitiet.Length; i++)
                //{
                //    if (mangchitiet[i].MaPhieuXuatHuy == temp.MaPhieuXuatHuy)
                //    {
                //        return Check(mangchitiet[i]);
                //    }
                //}
                return true;
            }
            catch
            {
                return false;
            }
        }


        public bool Check(Entities.PhieuXuatHuy phieuxuathuy)
        {
            bool kt = true;
            return kt;
        }
        #endregion



        #region Thoát
        void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        this.checkClose = true;
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion



        #region xử lý form
        private void frmXuLy_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            try
            {
                
                if (this.hanhDong == "Update")
                {
                    if (temp.TrangThai == true)
                    {

                       btnGhiLai.Enabled = false;
                        dgvHienThi.Enabled = false;

                        phieuxuathuy = null;
                      //  dgvHienThi.SelectedRows = -1;

                    }
                }
                txtMaKho.Focus();
                this.FormClosing += new FormClosingEventHandler(frmXuLy_FormClosing);
                HienThi();
            }
            catch
            {
            }
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            NewRow();
        }

        private void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                i = e.RowIndex;
            }
            catch { }
        }

        private void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                SuaHangHoa();
            }
            catch { }
        }

        void frmXuLy_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
            else
            {
                GetData(kiemtra);
            }
        }
        private void btnGhiLai_Click(object sender, EventArgs e)
        {
            GhiLai();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }
        private void txtSoLuong_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                XuLy_Enter(Common.Constants.PhieuXuatHuy.txtSoLuong);
            }
        }

        private void txtSoLuong_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int i = int.Parse(txtSoLuong.Text);
                if (i < 0)
                {
                    txtSoLuong.Text = "";
                }
            }
            catch
            {
                txtSoLuong.Text = "";
            }
        }
        public delegate void SelectData(string hanhdong);
        public SelectData Select;

        bool XuLyChuoiMaHangHoa()
        {
            try
            {
                bool kt = false;
                mahanghoa = txtMaHangHoa.Text.ToUpper();
                if (txtTenHangHoa.Text == "")
                {
                    foreach (Entities.HangHoa item in hanghoa)
                    {
                        if (item.MaHangHoa.ToUpper() == mahanghoa.ToUpper())
                        {
                            txtMaHangHoa.Text = item.MaHangHoa.ToUpper();
                            txtTenHangHoa.Text= item.TenHangHoa;
                            txtGiaNhap.Text = item.GiaNhap;
                            gianhap = item.GiaNhap;
                            tonsosach = item.SoLuong;
                            kt = true;
                        }
                    }
                    if (!kt)
                    {
                        MessageBox.Show(Common.MSG.MSG27(mahanghoa.ToUpper()), Common.MSG.TieuDe);
                        mahanghoa = "";
                        return false;
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        void XuLy_Enter(string str)
        {

            if (str == Common.Constants.PhieuXuatHuy.txtMaHangHoa)
            {
                if (makho.Length == 0 || makho == "NULL")
                {
                    MessageBox.Show(Common.MSG.MSG28(), Common.MSG.TieuDe);
                    return;
                }

                if (ValidateDetails())
                {
                    if (!XuLyChuoiMaHangHoa())
                    {
                        return;
                    }
                    txtSoLuong.Focus();
                }
            }
            if (str == Common.Constants.PhieuXuatHuy.txtSoLuong)
            {
                if (ValidateDetails())
                {
                    if (int.Parse(0 + txtSoLuong.Text) > 0)
                    {
                        mahanghoa = txtMaHangHoa.Text;
                        soluong = int.Parse(txtSoLuong.Text);
                        NewRow();
                       txtMaHangHoa.Text = null;
                       txtGiaNhap.Text = null;
                       txtTenHangHoa.Text = null;
                       txtSoLuong.Text = null;
                    }
                    else
                    {
                        MessageBox.Show("Số Lượng Phải Lớn Hơn 0");
                        txtSoLuong.Focus();
                    }
                }
            }
        }

        #endregion

        #region xử lý id
        public string getID()
        {
            string code = string.Empty;
            try
            {
                Entities.GetID id = new Entities.GetID();
                id.TableName = "PhieuXuatHuy";
                Entities.GetID get = Common.StaticVariable.InterfaceObject.SelectID(id);
                if (get.ColumnName != null)
                {
                    code = Common.Utilities.ProcessID(get.ColumnName);
                }
                else
                { code = "PXH_0001"; }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                code = "PXH_0001";
            }
            return code;
        }
        #endregion

        #region validate
        public bool Validate()
        {
            try
            {

                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool ValidateDetails()
        {
            try
            {
                if (txtMaHangHoa.Text.Length == 0 || txtMaHangHoa.Text == "<F4 - Tra cứu")
                {
                    MessageBox.Show(Common.MSG.MSG13(lblMaHangHoa.Text), Common.MSG.TieuDe);
                    txtMaHangHoa.Focus();
                    return false;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region xử lý object
        Entities.PhieuXuatHuy thevip;
       public DateTime ngaylap;
        public void XuLyDuLieu(Entities.PhieuXuatHuy thevip)
        {
            try
            {
                txtMaKho.Text = thevip.MaKho;
                txtTenKho.Text = thevip.TenKho;
                maPhieuXuatHuy = thevip.MaPhieuXuatHuy;
                manhanvien = thevip.MaNhanVien;
                tennhanvien = thevip.TenNhanVien;
                ngaylap = thevip.NgayLap;
                txtGhiChu.Text = thevip.GhiChu;
                textBox1.Text = thevip.Tongtien;
            }
            catch
            {
            }
        }
        // lấy obj
        public Entities.PhieuXuatHuy XuLyObject(string hanhDong)
        {
            try
            {
                if (Validate())
                {
                    thevip = new Entities.PhieuXuatHuy();
                    thevip.MaPhieuXuatHuy = maPhieuXuatHuy;
                    thevip.Deleted = false;
                    thevip.GhiChu = string.Empty;
                    thevip.MaKho = txtMaKho.Text;
                    //thevip.MaKho = makho;
                    //thevip.TenKho = tenkho;
                    thevip.HanhDong = hanhDong;
                    thevip.TenKho = txtTenKho.Text;
                    thevip.MaNhanVien = manhanvien;
                    thevip.TenNhanVien = tennhanvien;
                    thevip.Tongtien =tongtien; 
                    thevip.TrangThai = false;
                    thevip.NgayLap = ngaylap;
                    thevip.TaiKhoan = UtilitiesVariable.User;
                    return thevip;
                }
                return null;
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region Processing
        private Entities.ChiTietXuatHuy[] DanhSach;
        private Entities.ChiTietXuatHuy GoiHang = null;
        //lay ban ghi de sua hoac xem
        private void GetRow()
        {
            try
            {
                if (DanhSach == null) { return; }
                for (int i = 0; i < this.DanhSach.Length; i++)
                {
                    if (DanhSach[i].MaHangHoa == this.mahanghoa)
                    {
                        GoiHang = DanhSach[i];
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region hiển thị
        private void HienThi()
        {
            try
            {
                Entities.ChiTietXuatHuy nv = new Entities.ChiTietXuatHuy();
                nv.HanhDong = "3";
                nv.MaPhieuXuatHuy = maPhieuXuatHuy;
                DanhSach = Common.StaticVariable.InterfaceObject.SelectChiTietXuatHuy(nv);
                dgvHienThi.DataSource = DanhSach;
                this.HienThiListview();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                DanhSach = null;
                HienThiListview();
            }
        }

        private void HienThiListview()
        {
            try
            {
                if (DanhSach == null)
                {
                    Entities.ChiTietXuatHuy[] nv = new Entities.ChiTietXuatHuy[0];
                    dgvHienThi.DataSource = nv;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }

                dgvHienThi.Columns["MaHangHoa"].HeaderText = "mã hàng hóa";
                dgvHienThi.Columns["MaHangHoa"].Visible = true;
                dgvHienThi.Columns["TenHangHoa"].HeaderText = "Tên hàng hóa";
                dgvHienThi.Columns["TenHangHoa"].Visible = true;
                dgvHienThi.Columns["GiaNhap"].HeaderText = "Giá nhập";
                dgvHienThi.Columns["GiaNhap"].Visible = true;
                dgvHienThi.Columns["SoLuong"].HeaderText = "Số lượng";
                dgvHienThi.Columns["SoLuong"].Visible = true;
                if (dgvHienThi.RowCount > 0)
                {
                    Common.Utilities.CountDatagridview(dgvHienThi);
                    dgvHienThi.Focus();
                    dgvHienThi.Select();
                }
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion 

       
       
    }
}
